// 词汇
import { h5 } from '../../api/h5.js'
import Toast from '@vant/weapp/toast/toast'
let d = null

// 创建音频的上下文对象
const innerAudioContext = wx.createInnerAudioContext({
  useWebAudioImplement: true
})

Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPlay: true,
    w_url: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/audio/5692.mp3?sign=cc6cc2b497324c663b56e847ec532bbf&t=1692432698',
    r_url: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/audio/right.mp3?sign=b50a8167629d5e811a230e937f38678a&t=1692432675',
    time: 60 * 10000,
    timeData: {},
     // 是否获取焦点
     focus: false,
     // 需要获取焦点的序号
     focusIndex: 0,
    opt: {},
    arr: [],
    num: 0,
    str: '',
    text: '',
    cur: 0,
    i: 0,
    j: 0,
    indexs: [],
    isShow: false,
    isRight:'',
    tips: false,
    v: 0
  },
  closeFn(){
    this.setData({isPlay: false})
    wx.showToast({
      title: '关闭音效了',
      icon:'none'
    })
  },
  openFn(){
    this.setData({isPlay: true})
    wx.showToast({
      title: '开启音效了',
      icon:'none'
    })
  },
  rightFn(){
    innerAudioContext.src = this.data.r_url
    innerAudioContext.play()
  },

  wrongFn(){
    innerAudioContext.src = this.data.w_url
    innerAudioContext.play()
  },
  start() {
    const countDown = this.selectComponent('.control-count-down');
    countDown.start();
    if(d != null) {
      d = setTimeout(()=>{
        let num = this.data.num;
        num ++;
        this.getInitWord(num);
        clearTimeout(d);
        d = null;
      },500)
    }
    this.setData({isShow: true})
  
  },

  pause() {
    const countDown = this.selectComponent('.control-count-down');
    countDown.pause();
    clearTimeout(d);
    this.setData({isShow: false})
  },

  reset() {
    const countDown = this.selectComponent('.control-count-down');
    countDown.reset();
    this.getIndexsFn();
    // 初始化
    let num = this.data.indexs[this.data.num]
    this.getInitWord(num);
  },

  finished() {
    Toast('比赛结束');
    this.setData({isShow: false})
  },
  showTip(){
    this.setData({
      tips: true
    })
  },
   // 输入完成事件
   confirmListener (event) {
    let i =  this.data.i;
    let j = this.data.j;
  },
  // 监听输入
  addInput(e) {
    let v = e.detail.value;
    let index = e.currentTarget.dataset.index;
    // console.log({index})
    let arr = [...this.data.arr];
    arr[index] = v;
    // let str = arr.join('')
    this.setData({arr,text:arr.join(''),v: 0})
    if(arr.indexOf(" ") == -1) {
      let text = arr.join("");
      if(text == this.data.opt.word) {
        wx.showToast({
          title: '恭喜您,答对了。',
          icon: 'none'
        })
        this.setData({isRight: true, v: 100})
        if(this.data.isPlay){
          this.rightFn();
        }
      }
      else {
        wx.showToast({
          title: '这次答错了',
          icon: 'none'
        })
        // arr[index] = " ";
        this.setData({isRight: false,arr,text:arr.join('')})
        if(this.data.isPlay){
          this.wrongFn();
        }
        return;
      }
      if(d !=null) clearTimeout(d);
      d = setTimeout(()=>{
        let num = this.data.num
        num ++;
        num = num < this.data.indexs.length - 1 ? num : this.data.indexs.length - 1
        if(num == this.data.indexs.length -1) {
          Toast('这是最后一个词汇了');
        }
        this.setData({num,tips: false})
        this.getInitWord(this.data.indexs[num])
        this.setData({
          text:'',
          isRight: '',
          v: 0
        })
        clearTimeout(d);
        d = null;
      },2500)
    }

    // this.setData({focus: true,focusIndex: (index-0)})
  },
  // 处理词汇
  getInitWord(index) {
    let opt = h5[0].arr[index]
    let str = opt.word
    let arr = str.split("");
    let i = Math.floor(Math.random() * arr.length);
    let j = Math.floor(Math.random() * arr.length);
    arr[i] = " ";
    arr[j] = " ";
    this.setData({
      arr,
      str,
      opt,
      i,
      j
    })
  },

  // 获取音频文件
  getAudioFn(word){
    let src = `https://dict.youdao.com/dictvoice?audio=${word}&type=2`;
    innerAudioContext.src = src
    innerAudioContext.play()
  },

  // 随机坐标数组
  getIndexsFn(){
     let len = h5[0].arr.length;
     let indexs = [];
     const f = ()=> {
        let num = Math.floor(Math.random() * len);
        if(indexs.indexOf(num) > 0){
          return f()
        }
        return  num;
     }
     for(let i = 0 ; i < 20 ; i ++){
       let num = f();
       indexs.push(num)
     }
     this.setData({indexs,num: 0, tips: false})
  },

  // 换一组
  changeFn(){
    // 初始化
    this.getIndexsFn();
    let num = this.data.indexs[this.data.num]
    this.getInitWord(num);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
    // 初始化
    this.getIndexsFn();
    let num = this.data.indexs[this.data.num]
    this.getInitWord(num);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})